package zmee.pur.src.formplugin;

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.form.IFormView;
import kd.bos.form.field.BasedataEdit;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;
import kd.bos.list.ListShowParameter;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.util.EventObject;

/**
 * @Author: xw
 * @Date: 2022/11/15/14:35
 * @Description: 寻源方式=竞争性谈判的项目启动时，中标原则自动从竞争性谈判中标原则取数
 * 寻源方式=竞争性磋商的项目启动时，中标原则自动从竞争性磋商中标原则取数
 */
public class ProjectRuleBillPlugin extends AbstractBillPlugIn implements BeforeF7SelectListener {

    @Override
    public void beforeBindData(EventObject e) {
        super.beforeBindData(e);
        //获取父视图
        IFormView parentView = this.getView().getParentView();
        if (parentView != null) {
            boolean check = (boolean) this.getModel().getValue("zmee_check");
            if (check) {
                return;
            }
            this.getModel().setValue("zmee_check", true);
            //boolean dczb = parentView.getModel().getDataEntity().getBoolean("zmee_dczb");
            //if (dczb) {
            //    return;
            //}
            //项目信息.单据状态
            String billStatus = parentView.getModel().getDataEntity().getString("billstatus");
            //项目信息.寻源方式.number
            String sourceTypeNumber = parentView.getModel().getDataEntity().getDynamicObject("sourcetype").getString("number");
            if (billStatus == null || "".equals(billStatus) || "A".equals(billStatus)) {
                switch (sourceTypeNumber) {
                    case "C010406":
                        //获取竞争性谈判中标原则数据
                        QFilter tpFilter = new QFilter("billstatus", "=", "C");
                        String selectTpPro = "id,zmee_negotiaterule,zmee_sourcetype,zmee_winrule,zmee_winerqty,zmee_biderqty," +
                                "zmee_topsupplier,zmee_tpspreads,zmee_extfilter,zmee_scheme";
                        DynamicObject competitiveNegotiation = BusinessDataServiceHelper.loadSingleFromCache("zmee_tpzbyz", selectTpPro,
                                tpFilter.toArray());
                        if (competitiveNegotiation == null) {
                            return;
                        }
                        this.getModel().setValue("negotiaterule", competitiveNegotiation.getString("zmee_negotiaterule"));
                        this.getModel().setValue("sourcetype", competitiveNegotiation.getDynamicObject("zmee_sourcetype"));
                        this.getModel().setValue("winrule", competitiveNegotiation.getDynamicObject("zmee_winrule"));
                        this.getModel().setValue("scheme", competitiveNegotiation.getDynamicObject("zmee_scheme"));
                        this.getModel().setValue("winerqty", competitiveNegotiation.get("zmee_winerqty"));
                        this.getModel().setValue("biderqty", competitiveNegotiation.get("zmee_biderqty"));
                        this.getModel().setValue("topsupplier", competitiveNegotiation.get("zmee_topsupplier"));
                        this.getModel().setValue("zmee_tpspreads", competitiveNegotiation.get("zmee_tpspreads"));
                        this.getModel().setValue("extfilter", competitiveNegotiation.getDynamicObject("zmee_extfilter"));
                        break;
                    case "C010415":
                        //获取竞争性磋商中标原则数据
                        QFilter csFilter = new QFilter("billstatus", "=", "C");
                        String selectCsPro = "id,zmee_negotiaterule,zmee_sourcetype,zmee_winrule,zmee_winerqty,zmee_biderqty," +
                                "zmee_topsupplier,zmee_csscoregap,zmee_extfilter,zmee_scheme";
                        DynamicObject competitiveConsultation = BusinessDataServiceHelper.loadSingleFromCache("zmee_cszbyz", selectCsPro,
                                csFilter.toArray());
                        if (competitiveConsultation == null) {
                            return;
                        }
                        this.getModel().setValue("negotiaterule", competitiveConsultation.getString("zmee_negotiaterule"));
                        this.getModel().setValue("winrule", competitiveConsultation.getDynamicObject("zmee_winrule"));
                        this.getModel().setValue("sourcetype", competitiveConsultation.getDynamicObject("zmee_sourcetype"));
                        this.getModel().setValue("scheme", competitiveConsultation.getDynamicObject("zmee_scheme"));
                        this.getModel().setValue("winerqty", competitiveConsultation.get("zmee_winerqty"));
                        this.getModel().setValue("biderqty", competitiveConsultation.get("zmee_biderqty"));
                        this.getModel().setValue("topsupplier", competitiveConsultation.get("zmee_topsupplier"));
                        this.getModel().setValue("zmee_csscoregap", competitiveConsultation.get("zmee_csscoregap"));
                        this.getModel().setValue("extfilter", competitiveConsultation.getDynamicObject("zmee_extfilter"));
                        break;
                    default:
                }
            }
        }
    }

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        BasedataEdit extfilter = this.getView().getControl("extfilter");
        extfilter.addBeforeF7SelectListener(this);
    }

    @Override
    public void beforeF7Select(BeforeF7SelectEvent event) {
        String name = event.getProperty().getName();
        if ("extfilter".equals(name)) {
            //获取父视图
            IFormView parentView = this.getView().getParentView();
            if (parentView != null) {
                QFilter filter = null;
                //项目信息.寻源方式.number
                String sourceTypeNumber = parentView.getModel().getDataEntity().getDynamicObject("sourcetype").getString("number");
                if ("C010406".equals(sourceTypeNumber)) {
                    filter = new QFilter("group.number", "=", "SYS25");
                } else if ("C010415".equals(sourceTypeNumber)) {
                    filter = new QFilter("group.number", "=", "SYS26");
                }
                ListShowParameter showParameter = (ListShowParameter) event.getFormShowParameter();
                showParameter.getListFilterParameter().setFilter(filter);
            }
        }
    }
}
